package com.example.project.service.impl;

import com.example.project.model.FAQ;
import com.example.project.repository.CategoryRepository;
import com.example.project.repository.FAQRepository;
import com.example.project.service.FAQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FAQServiceImpl implements FAQService {

    @Autowired
    private FAQRepository faqRepository;

    @Autowired
    private CategoryRepository categoryRepository; // 假设你有 CategoryRepository

    // ... 其他方法实现 ...

    // 获取所有 FAQ
    @Override
    public List<FAQ> getAllFAQs() {
        return faqRepository.findAll();
    }

    // 添加 FAQ
    @Override
    public FAQ addFAQ(FAQ faq) {
        return faqRepository.save(faq);
    }

    // 更新 FAQ
    @Override
    public boolean updateFAQ(int id, FAQ faq) {
        FAQ existingFAQ = faqRepository.findById(id).orElse(null);
        if (existingFAQ != null) {
            existingFAQ.setCategoryId(faq.getCategoryId());
            existingFAQ.setQuestion(faq.getQuestion());
            existingFAQ.setAnswer(faq.getAnswer());
            faqRepository.save(existingFAQ);
            return true;
        }
        return false;
    }

    // 删除 FAQ
    @Override
    public boolean deleteFAQ(int id) {
        FAQ existingFAQ = faqRepository.findById(id).orElse(null);
        if (existingFAQ != null) {
            faqRepository.delete(existingFAQ);
            return true;
        }
        return false;
    }

    @Override
    public List<FAQ> getFAQsByCategoryId(int categoryId) {
        return List.of();
    }

    @Override
    public boolean categoryExists(int categoryId) {
        return categoryRepository.existsById(categoryId);
    }

    // 根据搜索关键词查询 FAQ
    @Override
    public List<FAQ> getFAQsBySearch(String search) {
        return faqRepository.findByQuestionContainingIgnoreCase(search);
    }
}